#include"mystdio.h"
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<malloc.h>
#include<unistd.h>
#include<assert.h>

MY_FILE *my_fopen(const char*path,const char*mode)
{
  int flag=0;
  if(strcmp(mode,"r")==0)
  {
    flag |= O_RDONLY;
  }
  else if(strcmp(mode,"w")==0)
  {
    flag |= (O_CREAT | O_WRONLY | O_TRUNC);
  }
  else if(strcmp(mode,"a")==0)
  {
    flag |= (O_CREAT | O_WRONLY | O_APPEND);
  }
  else{

  }
  //尝试打开文件
  mode_t m=0666;
  int fd=0;
  if(flag& O_CREAT)
  {
     fd=open(path,flag,m);
  }
  else fd=open(path,flag);
  if(fd<0) return NULL;

  //3.给用户返回MY_FILE对象
  MY_FILE*mf=(MY_FILE*)malloc(sizeof(MY_FILE));
  if(mf==NULL)
  {
    close(fd);
    return NULL;
  }

  //初始化MY_FILE对象
  mf->fd=fd;
  mf->flags=0;
  mf->flags=BUFF_LINE;//设置成行刷新
  memset(mf->outputbuffer,'\0',sizeof(mf->outputbuffer));
  mf->current=0;  

  //返回打开的文件
  return mf;

}
int my_fflush(MY_FILE*fp)
{
  assert(fp);
  int n=write(fp->fd,fp->outputbuffer,fp->current);
  fp->current=0;
  return 0;
}
size_t my_fwrite(const void*ptr,size_t size,size_t nmemb,MY_FILE*stream)
{
  if(stream->current==NUM) my_fflush(stream);
  //根据缓冲区的情况进行数据拷贝即可
  size_t user_size=size * nmemb;
  size_t my_size=NUM-stream->current;

  size_t writen=0;
  if(my_size>=user_size)
  {
    memcpy(stream->outputbuffer+stream->current,ptr,user_size);
    //更新数据字段
    stream->current+=user_size;
  }
  else
  {
    memcpy(stream->outputbuffer+stream->current,ptr,my_size);
    stream->current+=my_size;
    writen=size;
  }
  //开始刷新
  if(stream->flags&BUFF_ALL)
  {
    if(stream->current==NUM)
      my_fflush(stream);
  }
  else if(stream->current&BUFF_LINE)
  {
    if(stream->outputbuffer[stream->current-1]=='\n')
      my_fflush(stream);
  }
  else{

  }
  
}
int my_fclose(MY_FILE*fp)
{
  assert(fp);
  //冲刷缓冲区
  if(fp->current>0)
    my_fflush(fp);

  //关闭文件
  close(fp->fd);
  //释放堆空间
  free(fp);
  //指针设为NULL
  fp=NULL;
  return 0;
}
